package com.cooper.scraper.show;

import java.util.List;

import com.cooper.android.comm.Resource;
import com.cooper.scraper.R;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public abstract class GalleryFanartShow<T extends Resource> extends
		FanartsShow<T> implements OnItemSelectedListener, OnItemClickListener {

	private static final float TITLE_TEXT_SIZE = 25; // sp
	private static final float SUBTITLE_TEXT_SIZE = 15; // sp

	private RelativeLayout fanartsView = null;

	private Gallery gallery;
	private boolean galleryIsHide;
	private TextView mediaTypeView;
	private TextView mediaNameView;

	protected int galleryWidth;
	protected int galleryHeight;

	public GalleryFanartShow(Context context,
			FanartViewStrategy<T> fanartStrategy,
			FanartImgStrategy fanartImgStrategy, int width, int height,
			int watingImageRes) {
		super(context, fanartStrategy, fanartImgStrategy, watingImageRes);
		this.galleryWidth = width;
		this.galleryHeight = height;
		init();
	}

	@Override
	public int getFanartShowCount() {
		Log.d("GalleryFanartShow", "calc show Count:" + galleryWidth
				/ (int) fanartImgStrategy.getFanartWidth() + 4);
		return galleryWidth / (int) fanartImgStrategy.getFanartWidth() + 4;
	}

	@Override
	public void configurationChanged(Configuration configuration,
			Display display) {
		super.configurationChanged(configuration, display);
	}

	@Override
	protected void init() {
		super.init();
		galleryIsHide = false;
		fanartsView = new RelativeLayout(context);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, galleryHeight);
		lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		fanartsView.setLayoutParams(lp);

		gallery = createAdapterView(context);
		gallery.setOnItemSelectedListener(this);
		gallery.setOnItemClickListener(this);
		fanartsView.addView(gallery);
		Log.d("GalleryFanartShow", "view h:" + gallery.getHeight());

		final int mediaTypeViewId = 99;
		mediaTypeView = new TextView(context);
		mediaTypeView.setEllipsize(TruncateAt.MARQUEE);
		mediaTypeView.setSingleLine(true);
		mediaTypeView.setId(mediaTypeViewId);
		mediaTypeView.setTextColor(Color.DKGRAY);
		mediaTypeView.setPadding(0, 0, 0, 0);
		// mediaTypeView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
		// getFanartHeight() / 3 * 0.3f);
		mediaTypeView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
				SUBTITLE_TEXT_SIZE);

		lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		fanartsView.addView(mediaTypeView, lp);

		mediaNameView = new TextView(context);
		mediaNameView.setEllipsize(TruncateAt.MARQUEE);
		mediaNameView.setSingleLine(true);
		mediaNameView.setTextColor(Color.BLACK);
		mediaNameView.getPaint().setFakeBoldText(true);
		// mediaNameView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
		// getFanartHeight() / 3 / 2);

		mediaNameView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TITLE_TEXT_SIZE);

		mediaNameView.setPadding(0, 0, 0, 0);

		lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.ABOVE, mediaTypeViewId);
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		fanartsView.addView(mediaNameView, lp);

	}

	private List<ResourceCommandAction<T>> commands;

	@Override
	public void setCommands(List<ResourceCommandAction<T>> commands) {
		this.commands = commands;
		if (commands != null) {
			LinearLayout leftToolbar = new LinearLayout(context);
			LinearLayout rightToolbar = null;
			leftToolbar.setOrientation(LinearLayout.HORIZONTAL);
			int i = 0;
			for (ResourceCommandAction<T> command : commands) {
				ImageButton button = new ImageButton(context);
				button.setImageResource(command.getIconResId());
				button.setOnClickListener(new OnClickAction(command));
				button.setBackgroundDrawable(null);
				button.setAdjustViewBounds(true);
				button.setPadding(0, 0, 0, 0);

				int buttonHeight = Math.min(
						(int) (fanartImgStrategy.getFanartHeight() * 0.45),
						context.getResources().getDimensionPixelSize(
								R.dimen.gallery_button_max_height));
				//Log.d("GalleryFanartShow", "max height:" + maxHeight);
				button.setLayoutParams(new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT,buttonHeight));

				if (i < 3) {
					leftToolbar.addView(button);
				} else {
					if (rightToolbar == null) {
						rightToolbar = new LinearLayout(context);
						rightToolbar.setOrientation(LinearLayout.HORIZONTAL);
					}
					rightToolbar.addView(button);
				}
				i++;
			}

			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

			lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
			lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
			fanartsView.addView(leftToolbar, lp);

			if (rightToolbar != null) {
				lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
						RelativeLayout.TRUE);
				lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
						RelativeLayout.TRUE);
				fanartsView.addView(rightToolbar, lp);
			}
		}
	}

	class OnClickAction implements OnClickListener {
		private ResourceCommandAction<T> command;

		public OnClickAction(ResourceCommandAction<T> command) {
			super();
			this.command = command;
		}

		@Override
		public void onClick(View v) {
			T selectMedia = getSelectedResource();
			if (selectMedia != null)
				command.execute(selectMedia);
		}

	}

	@Override
	protected AdapterView<?> getAdapterView() {
		return gallery;
	}

	@Override
	public void setAdapter(FanartAdapter<T> fanartAdapter) {
		super.setAdapter(fanartAdapter);
		gallery.setAdapter(fanartAdapter);
	}

	@Override
	public void setSelection(int position, boolean animate) {
		if (gallery.getSelectedItemPosition() == position) {
			if (showListener != null)
				showListener.onMediaSelected(getSelectedResource());
			mediaNameView.setText(getSelectedResource().getTitle());
			mediaTypeView.setText(getSelectedResource().getSubtitle());
		} else
			gallery.setSelection(position, animate);
	}

	@Override
	public void hideOrShowFanartsView() {
		AnimationSet animationSet = new AnimationSet(true);
		TranslateAnimation translateAnimation;
		if (galleryIsHide) {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
					0f, Animation.RELATIVE_TO_SELF, 1.0f,
					Animation.RELATIVE_TO_SELF, 0f);
			translateAnimation.setDuration(700);
			fanartsView.setVisibility(View.VISIBLE);
		} else {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
					0f, Animation.RELATIVE_TO_SELF, 0f,
					Animation.RELATIVE_TO_SELF, 1.0f);
			translateAnimation.setDuration(1200);
			fanartsView.setVisibility(View.GONE);
		}

		animationSet.addAnimation(translateAnimation);
		// animationSet.setFillAfter(true);
		fanartsView.startAnimation(animationSet);
		galleryIsHide = !galleryIsHide;
	}

	@Override
	public void onItemSelected(AdapterView<?> adapter, View view, int id,
			long arg3) {
		@SuppressWarnings("unchecked")
		T result = (T) adapter.getItemAtPosition(id);
		itemSelected(result);
	}

	private void itemSelected(T selected) {
		if (showListener != null)
			showListener.onMediaSelected(selected);
		mediaNameView.setText(selected.getTitle());
		mediaTypeView.setText(selected.getSubtitle());
	}

	private void nothingSelected() {
		if (showListener != null)
			showListener.onMediaSelected(null);
		mediaTypeView.setText("");
		mediaNameView.setText("");
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		nothingSelected();
	}

	@Override
	public void resetSelected() {
		T result = getSelectedResource();
		if (result == null) {
			nothingSelected();
		} else {
			itemSelected(result);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int id, long arg3) {
		@SuppressWarnings("unchecked")
		T result = (T) adapter.getItemAtPosition(id);

		boolean action = true;
		if (showListener != null) {
			action = !showListener.onMediaClick(result);
		}
		if (action) {
			T selectMedia = getSelectedResource();
			if (selectMedia != null && selectMedia == result
					&& commands != null && !commands.isEmpty()) {
				commands.get(0).execute(selectMedia);
			}
		}

	}

	@Override
	public void removeFrom(ViewGroup from) {
		from.removeView(fanartsView);
	}

	@Override
	public void showTo(ViewGroup to) {
		to.addView(fanartsView);
	}

	protected abstract Gallery createAdapterView(Context context);

	@Override
	protected LayoutParams getFanartViewLayoutParams() {
		return new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

}
